package com.ruoyi.web.api;

import cn.hutool.http.HttpRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.otc.domain.*;
import com.ruoyi.otc.service.*;
import com.ruoyi.otc.vo.BusVo;
import com.ruoyi.web.api.vo.BusinessVo;
import com.ruoyi.web.constant.ApiConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "企业数字档案管理接口",tags = "企业数字档案管理接口")
@RestController
@RequestMapping(ApiConstant.API_V1+"business")
public class ApiBusinessController extends BaseController {

    @Autowired
    private IBusinessService businessService;
    @Autowired
    private IBazaarContributiveService bazaarContributiveService;
    @Autowired
    private IBazaarPersonnelService bazaarPersonnelService;

    @Autowired
    private IBazaarRegulatorService bazaarRegulatorService;

    @Autowired
    private IBazaarShareholderService bazaarShareholderService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private InvestorContributiveService investorContributiveService;
    @Autowired
    private IPersonnelService personnelService;
    @Autowired
    private ILegalService legalService;



    @Value("${gssx.labelAccessKeyId}")
    private String labelAccessKeyId;
    @Value("${gssx.governmentUrl}")
    private String governmentUrl;

    /**
     * 查询企业信息列表
     */
    @ApiOperation("查询企业信息列表")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody BusinessVo business)
    {
        startPage();
        List<Business> list = businessService.list(new QueryWrapper<Business>()
                .like(StringUtils.isNotBlank(business.getEntname()),"entname",business.getEntname())
                .orderByDesc("dom_district").orderByDesc("industry_co"));
//        for (Business bus : list) {
//            List<BazaarRegulator> regulators = bazaarRegulatorService.list(new QueryWrapper<BazaarRegulator>().eq("ent_name", bus.getEntname()).eq("dom", bus.getDom()));
//            if (regulators.size()>0){
//                bus.setDomDistrict(regulators.get(0).getDomDistrict());
//                bus.setIndustryCo(regulators.get(0).getIndustryCo());
//            }
//        }
        return getDataTable(list);
    }

    /**
     * 删除企业信息
     */
    @ApiOperation("删除企业信息")
    @PostMapping( "/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(businessService.deleteBusinessById(id));
    }

    @ApiOperation("政务数据接口")
    @PostMapping("/government")
    @ResponseBody
    public AjaxResult government(){
        String body = HttpRequest.post(governmentUrl)
                .header("accessKeyId", labelAccessKeyId)
                .header("Content-Type", "application/json")
                .timeout(20000)
                .execute().body();

        return AjaxResult.success(body);
    }

    @ApiOperation("企业政务数据接口")
    @PostMapping("/governmentLocal/{id}")
    @ResponseBody
    public AjaxResult governmentLocal(@PathVariable("id") Long id){
        BusVo busVo = new BusVo();

        Business business = businessService.getById(id);
        String creditCode = business.getUniscid();

        //市场监管委股东出资信息表
        List<BazaarContributive> bazaarContributiveList = bazaarContributiveService.selectListBycreditCode(creditCode);
        busVo.setBazaarContributiveList(bazaarContributiveList);

        //市场监管委企业人员信息表
        List<BazaarPersonnel> bazaarPersonnelList  = bazaarPersonnelService.selectListBycreditCode(creditCode);
        busVo.setBazaarPersonnelList(bazaarPersonnelList);

        //市场监管委企业基本信息表
        BazaarRegulator bazaarRegulator = new BazaarRegulator();
        bazaarRegulator.setOrgNo(creditCode);
        List<BazaarRegulator> bazaarRegulatorList = bazaarRegulatorService.selectBazaarRegulatorList(bazaarRegulator);
        busVo.setBazaarRegulatorList(bazaarRegulatorList);

        //市场监管委股东变更信息表BazaarShareholder
        List<BazaarShareholder>  bazaarShareholderList=bazaarShareholderService.selectListBycreditCode(creditCode);
        busVo.setBazaarShareholderList(bazaarShareholderList);

        //企业基本信息表
        List<Company>  companyList =companyService.selectListByinstitutionCode(creditCode);
        busVo.setCompanyList(companyList);


        //投资人及出资信息表
        List<InvestorContributive> contributiveList =   investorContributiveService.selectListBycertificateNumber(creditCode);
        busVo.setContributiveList(contributiveList);

        //主要人员信息表
//        if(!bazaarPersonnelList.isEmpty()){
//            List<Personnel> personnelList =  personnelService.selectListByBazaarId(bazaarPersonnelList.get(0).getEntId());
//            busVo.setPersonnelList(personnelList);
//        }

        //法定代表人（负责人）信息表
        List<Legal> legalList= legalService.selectListByshNumber(creditCode);
        busVo.setLegalList(legalList);

        return AjaxResult.success(busVo);
    }


    @ApiOperation("获取企业信息详情")
    @PostMapping( "/info/{id}")
    @ResponseBody
    public AjaxResult info(@PathVariable("id") Long id)
    {
        Business business = businessService.getById(id);
        return AjaxResult.success(business);
    }

}
